package cli.menu;

import cli.CLI;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.SortedSet;

import cli.tools.Access;
import cli.tools.Tools;

import shared.Receipt;
import shared.Record;



class SearchByPurchasingDate extends Function{
	
	private static final String[] reqDate = {
		 "-= Main menu > Search > Search by Purchasing Date =-",
		 "[b]: back [u]: undo [r]: redo [h]: home menu",
		 "",
		 "Please enter the range of dates you would like to search from: ",
		 "Format: From(DD.MM.YYYY)<space>To(DD.MM.YYYY) "
	};
	
	private static State back = State.SEARCHRECEIPTSELECTFILTER;
	private static State current = State.SEARCHBYPURCHASINGDATE;
	
	public static State runMenu(){
		
		cli.print(reqDate);
		
		String input = cli.nextLine();

		if(input.length() == 1){
			char charInput = input.charAt(0);
		if(tools.isHotKey(charInput))
			return hotKeyFunction(charInput);
		}
		
		return normFunction(input);
		
	}
	
	private static State normFunction(String input){
		
		String[] dates = input.split(" ");
		String fromDate = dates[0];
		String toDate = dates[1];
		int[] from = new int[3];
		int[] to = new int[3];
		
		try{
		from = tools.processDate(fromDate);
		to = tools.processDate(toDate);
		}
		catch(Exception e){
			cli.print(INVALIDDATE);
			return current;
		}
		System.out.println("[b]: back [u]: undo [r]: redo [h]: home menu");
		System.out
				.println("Select an item to view in detail by keying in its index");
		SortedSet<Record> results = cli.getApi().getDateRange(from[0], from[1], from[2],
				to[0], to[1], to[2]);//######Jan take note##########

		
		if ((results == null)|| results.isEmpty()){
			cli.print(noResults);
			return current;
		}

		Object[] temp = results.toArray();
		List<Record> searchResults = new ArrayList<Record>();
		for (Record record:results){
			if(record.getClass()==Receipt.class)
			searchResults.add((Record) record);
		}
		// Record toView = (Record)
		// Tools.selectFromList(searchResults,"searchByDate");
		Object choice = tools.selectFromList(searchResults, "searchByPurchasingDate");

		// if user selected back
		if (choice == null) {
			// go back to searchSelectFilter
			return back;
		}

		// if user's choice diverts to another menu (e.g, home)
		if (choice.getClass() == State.class) {
			return (State) choice;
		}

		// if user selected a good choice
		Record toView = (Record) choice;

		Access.viewRecord(toView);
		Access.modifyRecord(toView);

		return current;

		
		
	}
	
	
	private static State hotKeyFunction(char charInput){
		State nextState = current;
		
		switch (charInput) {
		case 'b':
		case 'B':
			nextState = back;
			break;
		case 'u':
		case 'U':
			tools.undo();
			break;
		case 'r':
		case 'R':
			tools.redo();
			break;
		case 'h':
		case 'H':
			nextState = State.MAINMENU;
			return nextState;
		default:
			break;
		}
		return nextState;
	}
	
	
}